Inputs: an array with the heapSize attribute and an index into the array.
When called: It is assumed that the binary tree’s rooted at and are max-heaps, but that might be smaller than it’s children, thus violating the max-heap property.
MAX-HEAPIFY lets the value of “float down” in the max-heap so that the subtree rooted at index obeys the max-heap property.
You basically just want to find the largest number out of the , and . Then if either of the children’s values are larger then you want to swap the largest with and recursively call MAX-HEAPIFY on the index where the largest value was before the swap.
Time Complexity
to compute the relationship between and the two children plus the time it takes to run MAX_HEAPIFY on the subtree (the recursion). The children’s subtrees each have size at most so the run time of MAX_HEAPIFY is:
This is an example of case 2 of the Master Theorem so the solution is